<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<c:import url="/includes/header.jsp"></c:import>
</head>
<body>
	<div class="container" id="order_container">
		<c:if test="${empty list}">
			<div class="row">
				<h4 class="alert alert-warning">
					<span style="margin-left: 50%">没有数据！</span>
				</h4>
			</div>
		</c:if>
		<form id="listForm" method="post"
			action="${pageContext.request.contextPath}/admin/deliver/list.do">
			<input type="hidden" name="status" value="${status }">
			<div class="row">
				<c:forEach items="${list}" var="s">
					<table class="table table-bordered table-hover" id="table_"
						style="text-align: left; margin-top: 20px; background-color: white;">

						<tr style="background-color: ${s.type=='ele'?'#0c9dd9':'#fbc700'}">
							<td>
								<div style="font-size: 16px" class="col-sm-9">
									<p>
										<span>订单来源</span><span style="margin-left: 20px">${s.type=='ele'?'饿了么':'美团外卖' }&nbsp;
											#${s.daySn }</span>
									</p>
									<h4>
										<span>订单ID:${s.id }</span>

									</h4>
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="font-size: 16px" class="col-sm-9">
									<p>
										<span>订单金额</span><span style="margin-left: 20px">${s.totalPrice }元</span>
									</p>
								</div>
								<div style="font-size: 16px" class="col-sm-2">
									<c:choose>
										<c:when test="${s.status==4 }">
											<p class="text-danger">总共用了${s.pendMinute}分钟</p>
										</c:when>
										<c:when test="${s.status==5 }">
											<p class="text-danger">${s.statusMsg}</p>
										</c:when>
										<c:otherwise>
											<p class="text-danger">已经等待${s.pendMinute}分钟</p>
										</c:otherwise>
									</c:choose>
									<p class="text-danger">
										已支付小费<span id="currentTip">${s.currentTip}</span>元,合计费用<span
											id="totalTip">${s.currentTip+s.deliverFee}</span>元
									</p>
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="font-size: 16px" class="col-sm-9">
									<p>
										<span>收货电话</span><span style="margin-left: 20px">${s.consigness}
											[${s.phone}]</span>
									</p>
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="font-size: 16px" class="col-sm-9">
									<p>
										<span>收货地址</span><span style="margin-left: 20px">${s.deliverPoi}</span>
									</p>
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="font-size: 16px" class="col-sm-6">
									<p>
										<span>发布时间</span><span style="margin-left: 20px"><fmt:formatDate
												value="${s.deliverAt}" pattern="yyyy-MM-dd HH:mm:ss" /></span><span
											style="float: right"></span>
									</p>
									<c:if test="${s.status==4 }">
										<p>
											<span>完成时间</span><span style="margin-left: 20px"><fmt:formatDate
													value="${s.statusChangeAt}" pattern="yyyy-MM-dd HH:mm:ss" /></span><span
												style="float: right"></span>
										</p>
									</c:if>
								</div>

								<div style="font-size: 16px" class="col-sm-6">
									<c:if test="${s.status==1 }">
										<p style="float: right">
											小费设置<input type="text" style="width: 80px" placeholder="输入金额">元
											<input type="button" class="btn btn-default tips"
												value="增加小费" data-id="${s.id }"> <input
												type="button" class="btn btn-default cancel" value="取消订单"
												data-id="${s.id }">
										</p>
									</c:if>
									<c:if test="${s.status==2 }">
										<p style="float: right">
											<input type="button" class="btn btn-default cancel"
												value="取消订单" data-id="${s.id }">
										</p>
									</c:if>
									<c:if test="${s.status==3 }">
										<p style="float: right">
											<!-- 										<input type="button" class="btn btn-default" value="通知客人配送状态" -->
											<%-- 											data-id="${s.id }"> --%>
										</p>
									</c:if>
									<c:if test="${s.status==4 }">
										<p style="float: right">
											<!-- 										<input type="button" class="btn btn-default" value="通知客人配送状态" -->
											<%-- 											data-id="${s.id }"> --%>
										</p>
									</c:if>
									<c:if test="${s.status==5 }">
										<p style="float: right">
											<!-- 										<input type="button" class="btn btn-default" value="取消发单" -->
											<!-- 											data-type="print"> -->
											<input type="button" class="btn btn-default send"
												data-orderId="${s.orderId }" value="重新发单" data-type="send">
										</p>
									</c:if>
									<c:if test="${s.status==-1 }">
										<p style="float: right">
											小费设置<input type="text" style="width: 80px" placeholder="输入金额">元
											<input type="button" class="btn btn-default tips"
												value="增加小费" data-id="${s.id }"> <input
												type="button" class="btn btn-default cancel" value="取消发单"
												data-id="${s.id }">
										</p>
									</c:if>
								</div>

							</td>
						</tr>
					</table>
				</c:forEach>
				<c:import url="/includes/page.jsp"></c:import>
			</div>
		</form>
	</div>
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="myModalLabel">取消快递单</h4>
				</div>
				<div class="modal-body" id="myModalBody">
					<img alt="timg.gif"
						src="${pageContext.request.contextPath}/images/admin/timg.gif"
						id="loadPng">
				</div>
				<div class="modal-footer" id="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
					<button type="button" id="btn-confirm" class="btn btn-primary">确定</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
</body>
</html>
<script type="text/javascript">
	$(function() {
		var loadpng = $("#loadPng");

		$(".tips")
				.click(
						function() {
							var val = $(this).prev().val();
							if (val == '' || val == 0) {
								bootbox.alert('请输入小费');
								return;
							}
							var id = $(this).attr('data-id');
							var data = {
								"id" : id,
								"tips" : val,
							};
							var url = '${pageContext.request.contextPath}/admin/deliver/tips.do';
							$.ajax({
								type : "post",
								dataType : "json",
								data : data,
								url : url,
								async : false,
								success : function(data) {
									if (data.code == "0") {
										bootbox.alert('添加小费成功', function() {
											window.location.reload();
										});
									} else {
										bootbox.alert("网络不稳定,运行失败");
									}
								},
								error : function() {
									bootbox.alert("网络不稳定,运行失败");
								}
							});
						});

		$(".cancel").click(
				function() {
					var id = $(this).attr('data-id');
					$("#modal-footer").show();
					$("#myModalBody").html(loadpng);
					$("#myModalBody").load(
							'${pageContext.request.contextPath}/admin/deliver/reason.do?id='
									+ id, function(response, status, xhr) {
								if (status == "success") {
									$('#myModal').modal('show')
								}
							});

				});

		$("#btn-confirm")
				.click(
						function() {
							var id = $("#myModalBody").find(
									"input[name='record']").val();
							var reasonId = $("#myModalBody").find(
									"input[name='reason']:checked").val();
							if (!reasonId) {
								bootbox.alert("请选择取消原因");
								return;
							}
							$("#modal-footer").hide();
							$("#myModalBody").html(loadpng);
							var data = {
								"id" : id,
								"reasonId" : reasonId
							};

							var url = '${pageContext.request.contextPath}/admin/deliver/cancel.do';
							$.ajax({
								type : "post",
								dataType : "json",
								data : data,
								url : url,
								async : true,
								success : function(data) {
									if (data.code == "0") {
										$('#myModal').modal('hide');
										bootbox.alert('取消成功', function() {
											window.location.reload();
										});
									} else {
										bootbox.alert("网络不稳定,运行失败");
									}
								},
								error : function() {
									bootbox.alert("网络不稳定,运行失败");
								}
							});

						});

		$(".send")
				.click(
						function() {
							var o = this;
							var orderId = $(this).attr("data-orderId");
							var dataType = $(this).attr("data-type");
							var data = {
								"orderId" : orderId,
								"dataType" : dataType
							};
							var url = '${pageContext.request.contextPath}/admin/order/handle.do';
							$.ajax({
								type : "post",
								dataType : "json",
								data : data,
								url : url,
								async : false,
								success : function(data) {
									if (data.code == 0) {
										$(o).remove();
									} else {
										bootbox.alert("网络不稳定,运行失败");
									}
								},
								error : function() {
									bootbox.alert("网络不稳定,运行失败");
								}
							});

						});

	});
</script>
